package framework.webserver;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.client.otdr.OTDRID;
import org.openems.entity.ChoiceEntity;
import org.openems.entity.Entity;

import framework.DomainService;
import framework.EntityStoreListener;

@Path("/otdrController")
public class ControllerResource {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String get() {
		return "Controller";
	}
	
	
	@PUT
    @Produces(MediaType.TEXT_PLAIN)
	public String put(CommandEntity command) {
		String c = command.command.toUpperCase();
		if (c.equals("START")) {
			final DomainService service = ServiceStore.get(command.service);
			final EntityStoreListener entityStoreListener = new EntityStoreListener() {
				@Override
				public void valueChanged(Entity entity) {
					MyWebSocket.fireMessage("Finished");
				}
			};
			service.addEntityStoreListener(entityStoreListener, OTDRID.get().TRACE);
			ChoiceEntity entity = (ChoiceEntity)service.getEntity(OTDRID.get().CONTROL);
			entity.setIndex(1);
			service.putEntity(entity);
		}
		else if (c.equals("STOP")) {
			
		}
		return "OK";
	}
}
